import { getInfoList, editInfo } from '@/api/getData'
const state = {
  list: [],
  query: {
    total: 0,
    size: 10,
    page: 1
  }
}

const mutations = {
  SET_LIST (state, data) {
    state.list = data
  },
  SET_QUERY (state, data) {
    const { total, page } = data
    state.query = Object.assign(state.query, {
      total, page
    })
  }
}

const actions = {
  _getInfoList ({ commit }, params) {
    return new Promise((resolve, reject) => {
      getInfoList(params).then(res => {
        const { error, message, payload } = res
        if (error === 200) {
          const { page, total, list } = payload
          commit('SET_LIST', list)
          commit('SET_QUERY', { total, page })
          resolve(payload)
        } else {
          reject(message)
        }
      })
    })
  },
  _editInfo ({ dispatch, state }, params) {
    return new Promise((resolve, reject) => {
      editInfo(params).then(res => {
        const { error, message } = res
        if (error === 200) {
          dispatch('_getInfoList', state.query)
          resolve(message)
        } else {
          reject(message)
        }
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
